package com.zuqiuzhandui.liveing.common;

import android.app.Application;

import com.code19.library.AppUtils;
import com.code19.library.GsonUtil;
import com.code19.library.L;
import com.facebook.stetho.Stetho;
import com.qiniu.pili.droid.streaming.StreamingEnv;
import com.squareup.leakcanary.LeakCanary;
import com.zuqiuzhandui.liveing.model.resultModel.CheckVerionRs;
import com.zuqiuzhandui.liveing.myInterfacce.Constant;
import com.zuqiuzhandui.liveing.net.HttpResult;
import com.zuqiuzhandui.liveing.net.UrlFactory;
import com.zuqiuzhandui.testing.myhelper.util.HashMapProxy;

import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.model.CheckEntity;
import org.lzh.framework.updatepluginlib.model.HttpMethod;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.model.UpdateParser;

import java.util.Map;

/**
 * Created by yq on 2016-09-23.
 */

public class MyApplication extends Application {
    static  MyApplication instense;


    @Override
    public void onCreate() {
        super.onCreate();
        instense=this;
        StreamingEnv.init(getApplicationContext());
//        LeakCanary.install(this);
        Stetho.initializeWithDefaults(this);
        initUpdate();
    }

    private void initUpdate() {
        final String tag="update";
        final String versionName= AppUtils.getAppVersionName(this,getPackageName());
        final int versionCode=AppUtils.getAppVersionCode(this,getPackageName());
        Map<String,String> map=new HashMapProxy<>();
        map.put("ver",versionName);
        map.put("appType","ANDROID");
        String url=new UrlFactory(Constant.VERSION_CHECK).setSecret(map).getUrl();
        L.d("update url is : "+url);
        L.d("update params is : "+map);
        UpdateConfig.getConfig()
                .checkEntity(new CheckEntity().setUrl(url).setMethod(HttpMethod.POST).setParams(map))
                .jsonParser(new UpdateParser() {
                    @Override
                    public Update parse(String httpResponse) {
                        L.d("servece call is : "+httpResponse);
                        CheckVerionRs result= (CheckVerionRs) GsonUtil.jsonToBean(httpResponse,CheckVerionRs.class);
                        Update update=new Update(httpResponse);

                        update.setUpdateTime(System.currentTimeMillis());
                        update.setIgnore(false);
                        if (result.getErrorCode()==0) {
                                //有新版本需要更新
                            CheckVerionRs.DataBean data = result.getData();
                            update.setVersionCode(versionCode+1);
                            update.setVersionName(data.getVer());
                            update.setUpdateContent(data.getMemo());
                            update.setForced(data.isForceUpdate());
                            update.setUpdateUrl(data.getUrl());
                        }
                        return update;
                    }
                });



    }

    public static MyApplication getInstance() {
        return instense;
    }




}
